C++游戏
text::C++
一、新建窗口项目
1 新建项目
official::Windows 和消息 - Win32
VSStudio——新建项目——Windows桌面应用程序——新建(此处项目名为PvZ)
#include "framework.h" #include "PvZ.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst; WCHAR szTitle[MAX_LOADSTRING]; WCHAR szWindowClass[MAX_LOADSTRING];
ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_PVZ, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) { return FALSE; }
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PVZ));
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
return (int)msg.wParam; }
|
2 MyRegisterClass
注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_PVZ);
|
3 InitInstance
保存实例句柄并创建主窗口
在此函数中,我们在全局变量中保存实例句柄并创建和显示主程序窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
MoveWindow(hWnd, 0, 0, 1280, 720, false);
|
4 WndProc
处理主窗口的消息
- WM_COMMAND:处理应用程序菜单
- WM_PAINT:绘制主窗口
- WM_DESTROY:发送退出消息并返回
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
|
5 About
“关于”框的消息处理程序
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
|
二、游戏逻辑基础
1 多线程
2 图像处理
三、脚本案例